// flags/user.go
package flags

import (
	"fmt"
	"github.com/sirupsen/logrus"
	"muziai-server/global"
	"muziai-server/models"
	"muziai-server/models/ctype"
	"muziai-server/utils/pwd"
)

type UserFlag struct {
}

func (UserFlag) CreateUser() {
	fmt.Printf("角色：%d 管理员   %d 用户\n", ctype.AdminRole, ctype.AuthRole)
	var role ctype.Role
	fmt.Scan(&role)

	fmt.Printf("用户名：")
	var username string
	fmt.Scan(&username)

	var user models.UserModel
	err := global.DB.Take(&user, "username = ?", username).Error
	if err == nil {
		logrus.Error("用户名已存在")
		return
	}

	fmt.Printf("密码：")
	var _pwd string
	fmt.Scan(&_pwd)

	fmt.Printf("再次输入密码：")
	var rePwd string
	fmt.Scan(&rePwd)

	if _pwd != rePwd {
		logrus.Error("两次密码不一致")
		return
	}

	hashPwd, _ := pwd.HashPassword(_pwd)

	err = global.DB.Create(&models.UserModel{
		Nickname:       username,
		Username:       username,
		Password:       hashPwd,
		RegisterSource: ctype.UsernameRegister,
		Role:           role,
	}).Error
	if err != nil {
		logrus.Error(err)
		return
	}
	logrus.Info("用户创建成功")
}
